<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/20 0020
 * Time: 4:23
 */

namespace app\admin\controller;
use app\admin\model\Teacher;
use think\Controller;
use app\admin\model\Admin as AdminModel;
use think\Db;
use think\facade\Session;
use think\Validate;

class Admin extends Controller
{
    //中间件登录限制
    protected $middleware = [
        'AdminCheck' => ['except' => ['admin_login'] ]
    ];
    //后台首页
    public function admin_index(){
        return $this->fetch();
    }
    //后台统计页
    public function admin_welcome(){
        $user=Db::table('user')->count();
        $teacher=Db::table('teacher')->count();
        $lab=Db::table('lab')->count();
        $lab_app=Db::table('lab_app')->count();
        $feedback=Db::table('feedback')->count();
        $notice=Db::table('notice')->count();
        $this->assign([
            'user'=>$user,
            'teacher'=>$teacher,
            'lab'    =>$lab,
            'lab_app'=>$lab_app,
            'feedback'=>$feedback,
            'notice'  =>$notice
        ]);
        return $this->fetch();
    }
    //后台登录
    public function admin_login(){
        if ($this->request->isAjax()) {
            $data=input('post.');
            $admin=new AdminModel();
            $teacher=new Teacher();
            switch ($data['statu']){
                case 0:
                    $teacher=$teacher->where([
                        'username'=>$data['username'],
                        'password'=>$data['password']
                    ])->find();
                    if ($teacher){
                        $msg=$this->msg(200,'登录成功,正在跳转...');
                        Session::set('teacher',$teacher);
                    }else{
                        $msg=$this->msg(400,'账号或密码错误');
                    }
                    return $msg;
                case 1:
                    $admin=$admin->where([
                        'username'=>$data['username'],
                        'password'=>md5($data['password'])
                    ])->find();
                    if ($admin){
                        $msg=$this->msg(200,'登录成功！正在跳转...');
                        Session::set('admin',$admin);
                    }else{
                        $msg=$this->msg(400,'账号或密码错误！');
                    }
                    return $msg;
                default:
                    return $this->msg(400,'请选择身份');
            }
        }
        return $this->fetch();
    }
    //用户注销
    public function admin_logout(){
        Session::delete('admin');
        Session::delete('teacher');
        $this->success('注销成功','/admin/login','',2);
    }
    //教师信息修改
    public function teacher_change(){
        $teacher_id=Session::get('teacher')['id'];
        if ($this->request->isPost()){
            $data=input('post.');
            $rule=[
                'realname'=>'require',
                'tel'     =>'require',
                'email'   =>'require|email'
            ];
            $validate=new Validate();
            $va=$validate->check($data,$rule);
            if ($va==true){
                $teacher=Db::table('teacher')
                    ->where('id',$teacher_id)
                    ->update([
                        'realname'=>$data['realname'],
                        'email'   =>$data['email'],
                        'tel'     =>$data['tel']
                        ]);
                if ($teacher){
                    $msg=$this->msg(200,'修改成功');
                }else{
                    $msg=$this->msg(400,'修改失败');
                }
            }else{
                $msg=$this->msg(400,$validate->getError());
            }
            return $msg;
        }
         $teacher=new Teacher();
         $teacher=$teacher
             ->where('id',$teacher_id)
             ->find();
         $labs=Db::table('lab')->where('teacher_id',$teacher_id)->column('name');
         $this->assign(
             [
                 'teacher'=>$teacher,
                 'labs'   =>$labs
             ]);
        return $this->fetch();
    }
    //教师头像上传
    public function teacher_img_uploads(){
        $img=request()->file('file');
        $info=$img->move('../public/uploads');
        if ($info){
            $id=Session::get('teacher')['id'];
            $teacher=Db::table('teacher')
                ->where('id',$id)
                ->update([
                    'img'=>$info->getSaveName()
                ]);
            if ($teacher){
                Session::set('teacher.img',$info->getSavename());
                $msg=[
                    'statu'=>200,
                    'msg'  =>'上传成功',
                    'file' =>$info->getSaveName()
                ];
            }else{
                $msg=$this->msg(400,'上传失败');
            }
        }else{
            $msg=$this->msg(400,'上传失败');
        }
        return $msg;
    }
    //修改密码
    public function change_password(){
        if ($this->request->isPost()){
            $data=input('post.');
            $rule=[
                'oldpassword|旧密码'=>'require',
                'newpassword|新密码'=>'require|length:6,12',
                'newpassword1|确认密码'=>'require|length:6,12|confirm:newpassword'
            ];
            $validate=new Validate();
            $va=$validate->check($data,$rule);
            if ($va){
                switch ($data['statu']){
                    //教师
                    case 0:
                        if (md5($data['oldpassword'])==Session::get('teacher')['password']){
                            $teacher=new Teacher();
                            $teacher=$teacher->save([
                                'password'=>md5($data['newpassword'])
                            ],['id'=>Session::get('teacher')['id']]);
                            if ($teacher){
                                $msg=$this->msg(200,'修改成功');
                            }else{
                                $msg=$this->msg(400,'修改失败');
                            }
                        }else{
                            $msg=$this->msg(400,'原密码不正确');
                        }
                    case 1: //管理员
                        if (md5($data['oldpassword'])==Session::get('admin')['password']){
                            $admin=new AdminModel();
                            $admin=$admin->save([
                                'password'=>md5($data['newpassword'])
                            ],['id'=>Session::get('admin')['id']]);
                            if ($admin){
                                $msg=$this->msg(200,'修改成功');
                            }else{
                                $msg=$this->msg(400,'修改失败');
                            }
                        }else{
                            $msg=$this->msg(400,'原密码不正确');
                        }

                }

            }else{
                $msg=$this->msg(400,$validate->getError());
            }
            return $msg;
        }
        return $this->fetch();
    }
    //信息
    public function msg($statu,$msg){
        return [
            'statu'=>$statu,
            'msg'   =>$msg
        ];
    }
}